package com.css.fxfzaqbz.modules.assess2.repository.impl;


import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.SecondDisastersEntity;
import com.css.fxfzaqbz.modules.assess2.entity.KeyplaceRiskassessment;
import com.css.fxfzaqbz.modules.assess2.entity.TaskContent;
import com.css.fxfzaqbz.modules.assess2.entity.UrgencysettleData;
import com.css.fxfzaqbz.modules.assess2.repository.UrgencysettleDataRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

@Repository
public class UrgencysettleDataRepositoryImpl implements UrgencysettleDataRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Override
    public UrgencysettleData getUrgencysettleDataByContent(String udContent, String udRelation) {
        try {
            String sql = "select * from sys_urgencysettle_data where ud_relation = '" + udRelation + "'  and del_flag = '" + YNEnum.N.toString() + "' and ud_content= '" + udContent + "'";
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void deleteUrgencysettleData(String udId, String userId, Timestamp currentTimestamp) {
        String sql = "update sys_urgencysettle_data set del_flag='" + YNEnum.Y.toString() + "',update_user='" + userId + "' ,update_time='" + currentTimestamp + "'  where ud_id ='" + udId + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void saveUrgencysettleData(UrgencysettleData urgencysettleData) {
        String sql = "insert into sys_urgencysettle_data(ud_id,del_flag,create_time,status,submiter,submit_phone," +
                "ud_content,ud_date,ud_people,ud_title,create_user,ud_relation,ud_file_id,data_message) values" +
                "(:udId,:delFlag,:createTime,:status,:submiter,:submitPhone,:udContent,:udDate,:udPeople,:udTitle,:createUser,:udRelation,:udFileId,:dataMessage)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(urgencysettleData);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public UrgencysettleData getUrgencysettleData(String id) {
        try {
            String sql = "select * from sys_urgencysettle_data where del_flag = '" + YNEnum.N.toString() + "' and ud_relation= '" + id + "'";
            UrgencysettleData urgencysettleData = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class));
            return urgencysettleData;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public UrgencysettleData getUrgencysettleDataById(String id) {
        String sql = "select * from sys_urgencysettle_data where del_flag = '" + YNEnum.N.toString() + "' and ud_id= '" + id + "'";
        UrgencysettleData urgencysettleData = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class));
        return urgencysettleData;
    }

    @Override
    public UrgencysettleData updateUrgencysettleData(UrgencysettleData urgencysettleData) {

        String sql = "update sys_urgencysettle_data set update_time=:updateTime,ud_file_id=:udFileId," +
                "ud_title=:udTitle,update_user=:updateUser " +
                "where ud_id=:udId";

        SqlParameterSource source = new BeanPropertySqlParameterSource(urgencysettleData);
        KeyHolder keyHolder = new GeneratedKeyHolder();
        namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return urgencysettleData;
    }

    @Override
    public List<TaskContent> getUrgencysettleData1(String id) {
            String sql = "select ud_id id,ud_content as content,ud_date as dateTime from sys_urgencysettle_data where del_flag = '" + YNEnum.N.toString() + "' and ud_relation= '" + id + "'";
            List<TaskContent> urgencysettleData = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TaskContent.class));
            return urgencysettleData;
    }

    @Override
    public List<UrgencysettleData> getUrgencysettleDataByIaId(String id) {
        String sql = "select * from sys_urgencysettle_data where ud_relation='" + id + "' and del_flag='" + YNEnum.N + "' and status='" + YNEnum.N + "'";
        List<UrgencysettleData> urgencysettleDatas = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class));
        return urgencysettleDatas;
    }

    @Override
    public List<UrgencysettleData> getUrgencysettleDataByActiveId(String id) {
        String sql = "select * from sys_urgencysettle_data where ud_relation='" + id + "' and del_flag='" + YNEnum.N + "' and status !='" + YNEnum.N + "'";
        List<UrgencysettleData> urgencysettleDatas = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class));
        return urgencysettleDatas;
    }

    @Override
    public List<UrgencysettleData> check(UrgencysettleData urgencysettleData) {
        String sql = "select * from sys_urgencysettle_data where ud_relation = '" + urgencysettleData.getUdRelation() + "' and del_flag = '"+YNEnum.N+"' and ud_title = '"+urgencysettleData.getUdTitle()+"' and ud_content ='"+urgencysettleData.getUdContent()+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class));
    }

    @Override
    public List<UrgencysettleData> getByEntity(UrgencysettleData urgencysettleData) {
        String sql = "select * from sys_urgencysettle_data where ud_relation = '" + urgencysettleData.getUdRelation() + "' and del_flag = '"+YNEnum.N+"' and ud_content = '"+urgencysettleData.getUdContent()+"' and status = '"+YNEnum.Y+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class));

    }

    @Override
    public void submitResult(String id, String description) {
        String sql = "update sys_urgencysettle_data set" +
                " ud_description = '"+description+"', " +
                " status = '"+YNEnum.Y+"' " +
                " where ud_id = '" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void saveDescription(List<String> ids, String description) {
        String sql = "update sys_urgencysettle_data set" +
                " ud_description = '"+description+"' " +
                " where ud_id in (:ids) ";
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        namedParameterJdbcTemplate.update(sql,map);
    }

    @Override
    public void removeResult(String id, String description) {
        String sql = "update sys_urgencysettle_data set" +
                " ud_description = '"+description+"', " +
                " submiter = '"+description+"', " +
                " submit_phone = '"+description+"', " +
                " status = '"+YNEnum.N+"' " +
                " where ud_id = '" + id + "'";
        jdbcTemplate.update(sql);

    }

    @Override
    public void finalSubmission(List<String> ids, String userName, String mobilePhone,Timestamp currentTimestamp) {
        String sql = "update sys_urgencysettle_data set" +
                " submiter = '"+userName+"', " +
                " submit_phone = '"+mobilePhone+"', " +
                " update_time = '"+currentTimestamp+"', " +
                " submit_time = '"+currentTimestamp+"', " +
                " status = '"+2+"' " +
                " where ud_id in (:ids) ";
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        namedParameterJdbcTemplate.update(sql,map);

    }

    @Override
    public List<UrgencysettleData> getAzByIaId(String iaId) {
        String sql = "select * from sys_urgencysettle_data where ud_relation='" + iaId + "' and del_flag='" + YNEnum.N + "' and status='" + 2 + "'";
        List<UrgencysettleData> urgencysettleDatas = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class));
        return urgencysettleDatas;
    }

    @Override
    public List<UrgencysettleData> getUrgencysettleDataByIaIdAndType(String iaId, String xjjaz) {
        String sql = "select * from sys_urgencysettle_data where ud_relation='" + iaId + "'  and ud_content= '"+xjjaz+"' and del_flag='" + YNEnum.N + "' and status='" + 2 + "'";
        List<UrgencysettleData> urgencysettleDatas = null;
        try {
            urgencysettleDatas = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class));
            return urgencysettleDatas;
        } catch (DataAccessException e) {

            return null;
        }

    }

    @Override
    public UrgencysettleData getUrgencyByIaId(String iaId) {
        String xjjazry="需紧急安置人员";
        String sql = "select * from sys_urgencysettle_data where del_flag = '" + YNEnum.N.toString() + "' and ud_relation= '" + iaId + "' and ud_content='"+xjjazry+"' and status ='1'";
        try {
            UrgencysettleData urgencysettleData = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class));
            return urgencysettleData;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public int getNumber(UrgencysettleData urgencysettleData) {
        String dzzhtj="需紧急安置人员";
        String sql="";
        sql="select u.* from sys_urgencysettle_data u  where  u.ud_relation='"+urgencysettleData.getUdRelation()+"' and u.ud_content='"+dzzhtj+"' and u.del_flag = '"+ YNEnum.N.toString() +"' and u.status ='1'";

        Integer total = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class)).size();

        return total;
    }

    @Override
    public void deleteTJ(String udRelation, Timestamp currentTimestamp, String userIDd) {
        String tj="需紧急安置人员";
        String sql="";
        sql="update sys_urgencysettle_data  set status ='0' ,update_user='"+userIDd+"',update_time='"+currentTimestamp+"' where  del_flag ='"+YNEnum.N.toString()+"' and  ud_relation = '"+udRelation+"' and ud_content='"+tj+"' and status='1'";
        jdbcTemplate.update(sql);
    }

    @Override
    public Boolean getNum(String iaId) {
        String dzzhtj="需紧急安置人员";
        String sql="";
        sql="select u.* from sys_urgencysettle_data u  where  u.ud_relation='"+iaId+"' and u.ud_content='"+dzzhtj+"' and u.del_flag = '"+ YNEnum.N.toString() +"' and u.status ='1'";

        Integer total = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UrgencysettleData.class)).size();
        if (total!=0){
            return true;
        }else {
            return false;
        }

    }


}
